package com.qch.docker.docker_demo.controller;

import com.qch.docker.docker_demo.pojo.GoodDO;
import com.qch.docker.docker_demo.pojo.StudentDO;
import com.qch.docker.docker_demo.repositry.GoodsRepository;
import com.qch.docker.docker_demo.repositry.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

/**
 * @Author qch
 * @Date 2021/6/23
 */
@RestController
@RequestMapping("/stu")
public class StudentController {
    @Autowired
    private StudentRepository repository;

    @GetMapping("/list")
    public List<StudentDO> queryGoods() {
        return repository.findAll(); }

    @RequestMapping ("/add/{name}/{age}")
    public String add(@PathVariable("name")String name,@PathVariable("age")String age) {
        StudentDO studentDO = new StudentDO(name,Integer.parseInt(age));
         repository.save(studentDO);
         return "success";
    }
    @RequestMapping ("/update/{id}/{age}")
    public String update(@PathVariable("id")String id,@PathVariable("age")String age) {
        StudentDO respOne = repository.getOne(Long.parseLong(id));
        if(respOne!=null)
        {
            respOne.setAge(Integer.valueOf(age));
        }


        repository.save(respOne);
        return "success";
    }
    @RequestMapping ("/del/{id}")
    public String del(@PathVariable("id")String id) {

        StudentDO studentDO = new StudentDO();
        studentDO.setId(Long.parseLong(id));
        repository.delete(studentDO);
        return "success";
    }
}
